﻿using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections;
using System.Net;
using System.Configuration;

namespace Gui.Functies.Filesharing
{
    public partial class Fileshare : UserControl
    {
        private int projectID;
        private string selected;
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn); 
        private Hashtable fileinfo = new Hashtable();
        private string usn = "alex";
        private string pass = "Huibenalexmoetengoedophunwachtwoordletten";
        private string ftp = "ftp://ftp.qq-design.com";

        public Fileshare()
        {
            
        }

        public Fileshare(int projectnummer)
        {
            projectID = projectnummer;
            InitializeComponent();
            Getfiles();
        }

        private void btnUpload_Click(object sender, EventArgs e)
        {
            Form upload = new Upload(projectID);
            upload.Show();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            try
            {
                selected = listViewFileOverzicht.SelectedItems[0].Text;
                Form download = new Download(projectID, selected);
                download.Show();
            }
            catch(ArgumentOutOfRangeException)
            {
                MessageBox.Show("Kies een bestand om te downloaden");
            }     
        }

        private void Getfiles()
        {
            try
            {
                grabConnection.Open();
                MySqlCommand filesGet = new MySqlCommand("SELECT DocName, Entrydate FROM doc_upload WHERE ProjectID='"+projectID+"'", grabConnection);

                MySqlDataReader reader = filesGet.ExecuteReader();

                while (reader.Read())
                {
                    fileinfo.Add(reader["Docname"], reader["Entrydate"]);
                }
                reader.Dispose();
                filesGet.Dispose();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }

            grabConnection.Close();
            Showfiles();
        }

        private void Showfiles()
        {
            foreach (string key in fileinfo.Keys)
            {
                ListViewItem item = new ListViewItem(String.Format("{0}", key));
                item.SubItems.Add(String.Format("{0}", fileinfo[key]));
                listViewFileOverzicht.Items.Add(item);
            }
        }

        //eerst uit ftp deleten daarna uit db deleten
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                selected = listViewFileOverzicht.SelectedItems[0].Text;
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftp + "/" + projectID + "/" + selected));

                //delete request aanmaken
                request.Method = WebRequestMethods.Ftp.DeleteFile;
                request.Credentials = new NetworkCredential(usn, pass);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;
                MessageBox.Show("Het bestand is verwijderd");
            }
            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("Kies een bestand om te verwijderen");
            }

            //string uit db verwijderen
            try
            {
                string datetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                MySqlCommand filepathUpload = new MySqlCommand("DELETE FROM doc_upload WHERE DocName ='"+selected+"'", grabConnection);
                grabConnection.Open();
                filepathUpload.ExecuteNonQuery();
            }
            catch (MySqlException f)
            {
                MessageBox.Show(f.ToString());
            }
            finally
            {
                grabConnection.Close();
            }

        }
    }
}
